<?php

namespace ticket\modules\backend\models;

use ticket\libs\Tools;
use Yii;

/**
 * This is the model class for table "tk_common_app".
 *
 * @property int $app_id
 * @property string $app_name
 * @property string $appid
 * @property string $appsecret
 * @property string $comid
 * @property int $state
 * @property int $app_info_id
 * @property string $close_reason
 * @property string $expire_time
 * @property string $add_time
 */
class CommonApp extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'tk_common_app';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            ['app_name', 'required', 'message' => 'app_name不能为空', 'on' => 'add'],
            ['app_name', 'string', 'message' => 'app_name必须是字符串',
                'min' => 2, 'tooShort' => 'app_name最短2位',
                'max' => 16, 'tooLong' => 'app_name最长16位'],
            ['appid', 'default', 'value' => ''],
            ['appid', 'string', 'message' => 'appid必须是字符串',
                'min' => 18, 'tooShort' => 'appid最短18位',
                'max' => 18, 'tooLong' => 'appid最长18位'],
            ['appsecret', 'default', 'value' => ''],
            ['appsecret', 'string', 'message' => 'appsecret必须是字符串',
                'min' => 32, 'tooShort' => 'appsecret最短32位',
                'max' => 32, 'tooLong' => 'appsecret最长32位'],
            ['comid', 'required', 'message' => 'comid不能为空'],
            ['comid', 'string', 'message' => 'comid必须是字符串',
                'min' => 8, 'tooShort' => 'comid最短8位',
                'max' => 8, 'tooLong' => 'comid最长8位'],
            ['state', 'default', 'value' => 0],
            ['state', 'integer', 'message' => 'state必须为数字，或数字字符串'],
            ['state', 'in', 'range' => array(0, 1, 2), 'message' => 'state值必须在0,1,2中选择'],
            ['app_info_id', 'required', 'message' => 'app_info_id不能为空'],
            ['app_info_id', 'integer', 'message' => 'app_info_id必须为数字，或数字字符串',
                'min'=>1, 'tooSmall' => 'app_info_id最小存储1',
                'max'=>65535, 'tooBig' => 'app_info_id最大存储65535'],
            ['close_reason', 'default', 'value' => ''],
            ['close_reason', 'string', 'message' => 'close_reason必须是字符串'],
            ['expire_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['expire_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'expire_time非yyyy-MM-dd HH:mm:ss格式'],
            ['add_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['add_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'add_time非yyyy-MM-dd HH:mm:ss格式'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'app_id' => 'App ID',
            'app_name' => 'App Name',
            'appid' => 'Appid',
            'appsecret' => 'Appsecret',
            'comid' => 'Comid',
            'state' => 'State',
            'app_info_id' => 'App Info ID',
            'close_reason' => 'Close Reason',
            'expire_time' => 'Expire Time',
            'add_time' => 'Add Time',
        ];
    }


    /*添加*/
    public function addApp($data)
    {
        // P($data);
        $newData['CommonApp'] = $data;
        $newData['CommonApp']['appid'] = 'TIK' . date('Y', time()) . Tools::generateRandomStr(3) . date('m', time()) . Tools::generateRandomStr(4) . date('d', time());
        $newData['CommonApp']['appsecret'] = md5('PAY' . $newData['CommonApp']['appid']);
        $newData['CommonApp']['app_info_id'] = $newData['CommonApp']['appInfoId'];
        $newData['CommonApp']['expire_time'] = date('Y-m-d 00:00:00',strtotime('+' . $newData['CommonApp']['buyTime'] . 'year +1day'));
        $newData['CommonApp']['add_time'] = date('Y-m-d H:i:s', time());
        // P($newData);
        if($this->load($newData) and $this->validate()){
            if($this->save(false)){
                return ['appid' => $newData['CommonApp']['appid'], 'appsecret' => $newData['CommonApp']['appsecret']];
            }
        }else
            return false;
    }



    /*** 通过appid 获取 app_id
     * @param $appId
     * @return mixed
     */
    public function getAppId($appId)
    {
        $aid = self::find()->where('appid = :aid',[':aid' => $appId])->select(['app_id'])->one()->app_id;
        return $aid;
    }


}
